package com.assessment.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.AssessmentIndexRepository;
import com.assessment.dao.AssessmentTemplateRepository;
import com.assessment.dto.AssessmentIndexDTO;
import com.assessment.entity.AssessmentIndex;
import com.assessment.entity.AssessmentTemplate;


@Service
@Transactional
public class AssessmentIndexService implements IAssessmentIndexService {

	@Autowired
	private AssessmentIndexRepository assessmentIndexRepository;
	@Autowired
	private AssessmentTemplateRepository assessmentTemplateRepository;
	
	public void saveOrUpdate(AssessmentIndex assessmentIndex) {
		// TODO Auto-generated method stub
		
		assessmentIndexRepository.save(assessmentIndex);
	}
	@Override
	public void saveOrUpdate(AssessmentIndexDTO assessmentIndexDTO) {
		// TODO Auto-generated method stub
		AssessmentTemplate assessmentTemplate=assessmentTemplateRepository.findOne(assessmentIndexDTO.getAssessmentTemplateId());
		AssessmentIndex assessmentIndex=new AssessmentIndex();
		BeanUtils.copyProperties(assessmentIndexDTO,assessmentIndex);
		assessmentIndex.setAssessmentTemplate(assessmentTemplate);
		assessmentIndexRepository.save(assessmentIndex);
	}
	
	public void delete(Long id) {
		// TODO Auto-generated method stub
		AssessmentIndex assessmentIndex=assessmentIndexRepository.findOne(id);
		if(assessmentIndex!=null) {
			assessmentIndex.setAssessmentTemplate(null);
			assessmentIndexRepository.save(assessmentIndex);
			assessmentIndexRepository.delete(assessmentIndex);
		}
		
	}

	
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<AssessmentIndex> assessmentIndexList=(List<AssessmentIndex>) assessmentIndexRepository.findAll(ids);
		if(assessmentIndexList!=null) {
			for(AssessmentIndex assessmentIndex :assessmentIndexList) {
				assessmentIndex.setAssessmentTemplate(null);
				assessmentIndexRepository.save(assessmentIndex);
			}
			assessmentIndexRepository.delete(assessmentIndexList);
		}

	}

	@Transactional(readOnly=true)
	public AssessmentIndex findOne(Long id) {
		// TODO Auto-generated method stub
		return assessmentIndexRepository.findOne(id);
	}

	@Transactional(readOnly=true)
	public Page<AssessmentIndexDTO> findAll(Long id,Pageable pageable) {
		// TODO Auto-generated method stub
		List<AssessmentIndexDTO> results = new ArrayList<AssessmentIndexDTO>();
		Page<AssessmentIndex> page=assessmentIndexRepository.findAll(id, pageable);
		for(AssessmentIndex assessmentIndex:page.getContent()) {
			AssessmentIndexDTO assessmentIndexDTO = new AssessmentIndexDTO();
        	BeanUtils.copyProperties(assessmentIndex, assessmentIndexDTO);
        	assessmentIndexDTO.setAssessmentTemplateId(assessmentIndex.getAssessmentTemplate().getId());
        	results.add(assessmentIndexDTO);
		}
		return new PageImpl<AssessmentIndexDTO>(results, pageable, page.getTotalElements());

	}




//	@Transactional(readOnly=true)
//	public Page<AssessmentIndex> findAll(Specification<AssessmentIndex> spec, Pageable pageable) {
//		// TODO Auto-generated method stub
//		return assessmentIndexRepository.findAll(spec, pageable);
//	}

}
